const webpack = require('atool-build/lib/webpack')
var CopyWebpackPlugin = require('copy-webpack-plugin');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var join = require("path").join;
var path = require('path');
var glob = require('glob');

module.exports = function (webpackConfig, env) {
  webpackConfig.babel.plugins.push('transform-runtime')
  webpackConfig.babel.plugins.push(['import', {
    libraryName: 'antd',
    style: true,
  }])

  // Support hmr
  if (env === 'development') {
    webpackConfig.devtool = '#eval'
    webpackConfig.babel.plugins.push(['dva-hmr', {
      entries: [
        './src/index.jsx',
      ],
    }])
  } else {
    webpackConfig.babel.plugins.push('dev-expression')
    webpackConfig.entry = { index: './src/index.jsx' }
  }

  //入口文件
  webpackConfig.entry = getEntry('src/**/index.jsx');

  // Don't extract common.js and common.css
  webpackConfig.plugins = webpackConfig.plugins.filter((plugin) => {
    return !(plugin instanceof webpack.optimize.CommonsChunkPlugin)
  })

  /**解决HtmlWebpackPlugin与atool-build内置处理html的loader冲突**/
  webpackConfig.module.loaders.forEach(function (loader,i) {
    // var str = JSON.stringify(loader);
    // if(str.indexOf("file?") != -1){
    //   webpackConfig.module.loaders.splice(i,1);
    // }
    //
    // if(loader.test.toString() == '/\.js$/') {
    //   webpackConfig.module.loaders.splice(i,1);
    // }
    // atool-build 0.9版本需要采用这种方式
    if(loader.test.toString() === '/\\.html?$/'){
      loader.loader = 'html';
    }
  });

  var pages = Object.keys(getEntry('src/**/*.html'));
  pages.forEach(function(pathname) {
    var conf = {
      filename: pathname + '.html', //生成的html存放路径，相对于path
      template: 'src/' + pathname + '.html', //html模板路径
      inject: false,	//js插入的位置，true/'head'/'body'/false
      /*
       * 压缩这块，调用了html-minify，会导致压缩时候的很多html语法检查问题，
       * 如在html标签属性上使用{{...}}表达式，所以很多情况下并不需要在此配置压缩项，
       * 另外，UglifyJsPlugin会在压缩代码的时候连同html一起压缩。
       * 为避免压缩html，需要在html-loader上配置'html?-minimize'，见loaders中html-loader的配置。
       */
      // minify: { //压缩HTML文件
      // 	removeComments: true, //移除HTML中的注释
      // 	collapseWhitespace: false //删除空白符与换行符
      // }
    };
    if (pathname in webpackConfig.entry) {
      //conf.favicon = 'src/imgs/favicon.ico';
      conf.inject = 'body';
      conf.chunks = ['vendors', pathname];
      conf.hash = true;
    }
    webpackConfig.plugins.push(new HtmlWebpackPlugin(conf));
  });

  // Support CSS Modules
  // Parse all less files as css module.
  webpackConfig.module.loaders.forEach((loader) => {
    if (typeof loader.test === 'function' && loader.test.toString().indexOf('\\.less$') > -1) {
      loader.include = /node_modules/
      loader.test = /\.less$/
    }
    if (loader.test.toString() === '/\\.module\\.less$/') {
      loader.exclude = /node_modules/
      loader.test = /\.less$/
    }
    if (typeof loader.test === 'function' && loader.test.toString().indexOf('\\.css$') > -1) {
      loader.include = /node_modules/
      loader.test = /\.css$/
    }
    if (loader.test.toString() === '/\\.module\\.css$/') {
      loader.exclude = /node_modules/
      loader.test = /\.css$/
    }
  })

  return webpackConfig
}

function getEntry(globPath) {
  var files = glob.sync(globPath);
  var entries = {}, entry, dirname, basename, pathname, extname;
  for (var i = 0; i < files.length; i++) {
    entry = files[i];
    dirname = path.dirname(entry);
    extname = path.extname(entry);
    basename = path.basename(entry, extname);
    pathname = path.join(dirname, basename);
    pathname = pathname.substr(4);
    pathname = pathname.replace(/\\/g, '/');
    //pathname = pathname.replace('\\', '/');
    entries[pathname] = ['./' + entry];
  }
  return entries;
}
